package com.example.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


/**
 * 时间、日期、本地化 相关工具类
 * 
 * @author  2013-6-24
 * @version 2015/1/28
 */
public final class TimeUtils {
	public static final String DEF_DATE_TIME_TEMPLATE = "yyyy/MM/dd HH:mm:ss";
	public static final String DEF_DATE_TEMPLATE = "yyyy/MM/dd";
	public static final String DEF_TIME_TEMPLATE = "HH:mm:ss";
	public static final long DAY_MILLIS = 1000L * 3600 * 24;
	
	private static final long MILLIS_20140101 = 1388505600000L; // 2014/1/1的毫秒值
	private static SimpleDateFormat sdf_time = new SimpleDateFormat(DEF_TIME_TEMPLATE, getChina());
	private static SimpleDateFormat sdf_date = new SimpleDateFormat(DEF_DATE_TEMPLATE, getChina());

	/**
	 * @return 当前地域
	 */
	public static Locale getChina(){
		return Locale.CHINA;
	}

	/**
	 * @return 形式  2013/12/12 12:23:32
	 */
	public static String getDateTimeNow() {
		return getNow(DEF_DATE_TIME_TEMPLATE);
	}
	
	/**
	 * @return 形式 2013-04-21
	 */
	public static String getDateNow() {
		return getNow(DEF_DATE_TEMPLATE);
	}

	/**
	 * @return 形式 3.2
	 */
	public static String getDateNowM() {
		return getNow("M.d");
	}
	
	public static String getTimeNow() {
		return getNow(DEF_TIME_TEMPLATE);
	}
	
	/**
	 * @return 形式  12:23:32
	 */
	public static String getTimeNow2() {
		return sdf_time.format(new Date());
	}

	/**
	 * @param template 格式化模板
	 * 
	 * @return 按模板格式化后的结果
	 */
	public static String getNow(String template) {
		SimpleDateFormat sdf = new SimpleDateFormat(template, getChina());
		return sdf.format(new Date());
	}
	
	/**
	 * @return 形式  yyyy-MM-dd HH:mm:ss
	 */
	public static String formatDate(long ms) {
		return formatDate(ms, DEF_DATE_TIME_TEMPLATE);
	}
	
	public static String getDateTime(long ms) {
		return formatDate(ms, DEF_DATE_TIME_TEMPLATE);
	}

	public static String getTime(long ms) {
		return formatDate(ms, DEF_TIME_TEMPLATE);
	}

	public static String getDate(long ms) {
		return formatDate(ms, DEF_DATE_TEMPLATE);
	}
	
	public static String formatDate(long ms, String template) {
		// 取系统时间
		SimpleDateFormat format = new SimpleDateFormat(template, getChina());
		return format.format(new Date(ms));
	}
	
	/**
	 * 获取基于 2014 年的当前天数(从0开始)
	 */
	public static int getDayOfYearBase2014() {
		Calendar c = Calendar.getInstance(getChina());
		return (int) ((c.getTimeInMillis() - MILLIS_20140101) / DAY_MILLIS);
	}

	/**
	 * 将基于2014年的天数转换为实际日期(从0开始)
	 */
	public static String dayOfYearBase2014ToDate(int day) {
		Date date = new Date(MILLIS_20140101 + DAY_MILLIS * day);
		return sdf_date.format(date);
	}
	
	/**
	 * 今年中的第几天
	 */
	public static int getDayOfYear() {
		return Calendar.getInstance(getChina()).get(Calendar.DAY_OF_YEAR);
	}
	/**
	 * 天数转换成今年的日期
	 */
	public static String dayOfYearToDate(int day) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_YEAR, day);
		return sdf_date.format(c.getTime());
	}
	/**
	 * 小时,24小时
	 */
	public static int getHourOfDay() {
		return Calendar.getInstance(getChina()).get(Calendar.HOUR_OF_DAY);
	}
	
	public static long currentTimeMillis() {
		return System.currentTimeMillis();
	}
}
